<template>
    <div>
        <!--顶部输入框-->
        <div class="permissManTool">
            <el-input placeholder="请输入角色英文名" v-model="role.name" size="small">
                <template slot="prepend">ROLE_</template>
            </el-input>
            <el-input placeholder="请输入角色中文名" v-model="role.nameZh" size="small"
                      @keydown.enter.native="addRole"></el-input>
            <el-button icon="el-icon-plus" type="primary" size="small" @click="addRole">添加角色</el-button>
        </div>
        <!--折叠面板-->
        <div class="permissManToolMain">
            <el-collapse accordion @change="change" v-model="activeName">
                <el-collapse-item :title="r.nameZh" :name="r.id" v-for="(r,index) in roles" :key="index">
                    <el-card class="box-card">
                        <div slot="header" class="clearfix">
                            <span>可访问资源</span>
                            <el-button style="float: right; padding: 3px 0;color: red"
                                       type="text"
                                       icon="el-icon-delete"
                                       @click="deleteRole(r)"
                            ></el-button>
                        </div>
                        <!--可访问资源下的菜单内容-->
                        <div>
                            <el-tree
                                    show-checkbox
                                    :data="allMenus"
                                    :props="defaultProps"
                                    ref="tree"
                                    :key="index"
                                    :default-checked-keys="selectMenus"
                                    node-key="id">
                            </el-tree>
                            <div style="display: flex;justify-content: flex-end">
                                <el-button size="mini" @click="cancelUpdate">取消修改</el-button>
                                <el-button size="mini" type="primary" @click="updateMenuRole(r.id,index)">确认修改
                                </el-button>
                            </div>

                        </div>
                    </el-card>
                </el-collapse-item>
            </el-collapse>
        </div>
    </div>
</template>

<script>
    export default {
        name: "PerMissManager",
        data() {
            return {
                //顶部输入框绑定数据
                role: {
                    name: '',
                    nameZh: '',
                },
                roles: [],
                //全部菜单数据
                allMenus: [],
                defaultProps: {
                    children: 'children',
                    label: 'name',
                },
                //选中的数组
                selectMenus: [],
                //默认打开的折叠面板
                activeName: -1,
            }
        },
        methods: {
            /**
             * 初始化数据
             */
            initRoles() {
                this.getRequest('/system/admin/getAllRoles').then(res => {
                    if (res) {
                        this.roles = res;
                    }
                })
            },

            /**
             * 折叠面板点击事件
             * @param rid
             */
            change(rid) {
                if (rid) {
                    this.initAllMenus();
                    this.initSelectMenus(rid);
                }
            },

            /**
             * 初始化所有菜单
             */
            initAllMenus() {
                this.getRequest('/system/basic/permiss/menus').then(res => {
                    if (res) {
                        this.allMenus = res;
                    }
                })
            },


            /**
             * 初始化角色拥有的菜单
             * @param rid 角色id
             */
            initSelectMenus(rid) {
                this.getRequest('/system/basic/permiss/mid/' + rid).then(res => {
                    if (res) {
                        this.selectMenus = res;
                    }
                })
            },

            /**
             * 更新角色菜单
             * @param rid 角色id
             * @param index 循环中的数组的索引
             */
            updateMenuRole(rid, index) {
                let tree = this.$refs.tree[index];
                let selectKeys = tree.getCheckedKeys(true);
                let url = '/system/basic/permiss/updateMenuRole/?rid=' + rid;
                selectKeys.forEach(key => {
                    url += '&mids=' + key;
                });
                this.putRequest(url).then(res => {
                    if (res) {
                        this.activeName = -1;
                    }
                })
            },

            /**
             * 取消更新
             */
            cancelUpdate() {
                this.activeName = -1;
            },

            /**
             * 添加角色
             */
            addRole() {
                if (this.role.name && this.role.nameZh) {
                    this.postRequest('/system/basic/permiss/addRole', this.role).then(res => {
                        if (res) {
                            this.role.name = '';
                            this.role.nameZh = '';
                            this.initRoles();
                        }
                    })
                } else {
                    this.$message.error("请补充完整")
                }
            },

            /**
             * 删除角色
             * @param role 角色
             */
            deleteRole(role) {
                this.$confirm('此操作将永久删除【' + role.nameZh + '】角色, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.deleteRequest('/system/basic/permiss/deleteRole/' + role.id).then(res => {
                        if (res) {
                            this.initRoles();
                        }
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });

            }

        },
        mounted() {
            this.initRoles();
        }
    }
</script>

<style>
    .permissManTool {
        display: flex;
        justify-content: flex-start;
    }

    .permissManTool .el-input {
        width: 300px;
        margin-right: 6px;
    }

    .permissManToolMain {
        margin-top: 10px;
        width: 800px;
    }
</style>
